package com.xq.controller.admin.login;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xq.config.SessionContants;
import com.xq.entity.User;
import com.xq.ex.commom.err.ValidationException;
import com.xq.ex.commom.valid.KAssert;
import com.xq.service.user.IUserService;
import com.xq.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author 徐柯
 * @Title:
 * @Package
 * @Description:
 * @date 2021/4/1616:46
 */
@Controller
public class AdminLoginController {


    @Autowired
    private IUserService userService;

    /**
     * 跳转到后台登录
     *
     * @return
     */
    @PostMapping("/admin/logined")
    @ResponseBody
    public String login(@RequestBody User uservo, HttpSession session) {
        KAssert.isEmpty(uservo.getUsername(), 601, "请输入用户名");
        KAssert.isEmpty(uservo.getPassword(), 601, "请输入密码");
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", uservo.getUsername());
        queryWrapper.eq("password", MD5Util.md5slat(uservo.getPassword()));
        User user = userService.getOne(queryWrapper);
        if (user == null) {
            throw new ValidationException(601,"用户名或者密码有误!!");
        }
        session.setAttribute(SessionContants.LOGIN_USER, user);
        return "success";
    }


    @GetMapping("/admin/logout")
    public String loginout(HttpSession session) {
        session.invalidate();
        return "redirect:/admin/login";
    }
}
